package com.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/3/12.
 */
public class MyFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter---------");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Object username = request.getSession().getAttribute("loginUser");
        String uri = request.getRequestURI();
        System.out.println("username= " + username + ",url=" + uri);
//        if (null == username || username.equals("")) {
//            //判断获取的路径不为空且不是访问登录页面或执行登录操作时跳转
//            if (uri != null && !uri.equals("") && (uri.indexOf("login") < 0)) {
//                response.sendRedirect("/login/toLogin.html");
//                return;
//            }
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }
}
